In this step you create a material which provides the Gaussian blur effect.
The starting point of this tutorial is the Blur.kzproj Kanzi Studio project file stored in the <KanziWorkspace>/Tutorials/Blur/Start directory.
The <KanziWorkspace>/Tutorials/Blur/Completed directory contains the completed Kanzi Studio project of this tutorial.
In this section you first create a material type which supports the directional Gaussian blur effect, then you create a material you can use to apply the effect to 3D content.
To create a material for the blur effect:
precision mediump float; uniform sampler2D Texture0; uniform lowp float BlendIntensity; uniform mediump vec2 kzTextureSize0; // Defines the direction (x or y axis) along which to apply the blur. uniform lowp vec2 BlurDirection; // Determines the strength of the blur. uniform mediump float BlurRadius; // Defines the texture coordinate attribute passed from the vertex shader. varying mediump vec2 vTexCoord; vec4 gaussianBlur(mediump vec2 coord, lowp vec2 dir) { // Defines the one-dimensional Gaussian Kernel with 9 samples. float GAUSSIAN_KERNEL[9]; GAUSSIAN_KERNEL[0] = 0.028532; GAUSSIAN_KERNEL[1] = 0.067234; GAUSSIAN_KERNEL[2] = 0.124009; GAUSSIAN_KERNEL[3] = 0.179044; GAUSSIAN_KERNEL[4] = 0.20236; GAUSSIAN_KERNEL[5] = 0.179044; GAUSSIAN_KERNEL[6] = 0.124009; GAUSSIAN_KERNEL[7] = 0.067234; GAUSSIAN_KERNEL[8] = 0.028532; vec2 texel = 1.0/kzTextureSize0; vec4 sum = vec4(0.0); // Get the original texture coordinate for this fragment. vec2 tc = coord; // Get the amount to blur. float blur = BlurRadius; // Set the amount of blur in the horizontal direction. float hstep = dir.x*texel.x; // Set the amount of blur in the vertical direction. float vstep = dir.y*texel.y; // Sample the texture 9 times for every fragment. for(int i = 0; i < 9; i++) { float pixelOffset = (float(i) - floor(9.0 * 0.5)); mediump vec2 coord = vec2(tc.x + pixelOffset * blur * hstep, tc.y + pixelOffset * blur * vstep); sum += texture2D(Texture0, coord) * GAUSSIAN_KERNEL[i]; } return sum; } void main() { gl_FragColor = gaussianBlur(vTexCoord, BlurDirection) * BlendIntensity; }
To learn more about materials and material types, see Material types and materials.
To learn more about shaders, see Shaders.